package Group9.TodoManager.Actions;

import Group9.TodoManager.Data.Interface.ITableModel;
import Group9.TodoManager.Data.NodeItem;
import Group9.TodoManager.DataStore.XMLQuery;
import Group9.TodoManager.DataStore.Repository;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import javax.swing.event.TreeSelectionEvent;

/**
 * LoadAction is used to load the table
 * @author volkan cambazoglu
 * @author Rupesh
 */
public class LoadAction<T> extends TodoManagerAction {
	
    private ITableModel<T> tableModel;
    private Repository<T> repository;
    private String currentNode = "NONE";

    /**
     * Constructor. Creates an instance of the LoadAction class.
     * @param table tableModel
     * @param repo repository
     * @param mnemonic mnemonic of the action
     */
    public LoadAction(ITableModel<T> table, Repository<T> repo, int mnemonic){
        putValue(Action.NAME, "Load Tasks");
        putValue(Action.MNEMONIC_KEY, mnemonic);
        tableModel = table;
        repository = repo;
    }

    /**
     * When this method will be executed, it will take the necessary action.
     * @param e event
     */
    @Override
    public void actionPerformed(ActionEvent e){        
    }

    /**
     * Set the data source of the table model.
     * @param params TreeSelectionEvent
     */
    @Override
    public void executeAction(Object params){
        if(params == null) {
        	tableModel.setDataSource(repository.getItem(new XMLQuery(currentNode)));
        }
        else if(params instanceof TreeSelectionEvent) {
            TreeSelectionEvent e = (TreeSelectionEvent)params;
            String value = ((NodeItem) e.getNewLeadSelectionPath().
                    getLastPathComponent()).getNodeValue().toString();
            if(!currentNode.equalsIgnoreCase(value)) {
                currentNode = value;
                tableModel.setDataSource(repository.getItem(new XMLQuery(currentNode)));
            }
        }
        else {
        	
        }
    }   
}